package com.yeyks.consumer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.yeyks.common.appcommon.dto.platform.AppFeedbackDTO;
import com.yeyks.common.appcommon.param.platform.QueryFeedbackParam;
import com.yeyks.common.appcommon.param.platform.SetFeedbackParam;
import com.yeyks.common.appcommon.result.CommonPage;
import com.yeyks.common.appcommon.result.CommonResult;
import com.yeyks.common.utils.apputil.bean.MyBeanUtils;
import com.yeyks.consumer.dal.domain.Feedback;
import com.yeyks.consumer.dal.dao.FeedbackMapper;
import com.yeyks.consumer.service.FeedbackService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;


/**
 * <p>
 * 反馈表 服务实现类
 * </p>
 *
 * @author yun
 * @since 2019-06-25
 */
@Service
public class FeedbackServiceImpl extends ServiceImpl<FeedbackMapper, Feedback> implements FeedbackService {

    @Override
    public CommonResult<CommonPage<AppFeedbackDTO>> queryPageByParam(QueryFeedbackParam param) {

        CommonPage<AppFeedbackDTO> appFeedbackDTOCommonPage = new CommonPage<>();
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<Feedback> feedbackList = baseMapper.selectList(new QueryWrapper<>());
        if (!CollectionUtils.isEmpty(feedbackList)) {
            CommonPage<Feedback> feedbackCommonPage = CommonPage.restPage(param.getPageNum(), param.getLastId(), feedbackList);
            appFeedbackDTOCommonPage = MyBeanUtils.convertCommonPageList(feedbackCommonPage, AppFeedbackDTO.class);
        }

        return CommonResult.success(appFeedbackDTOCommonPage);
    }

    @Override
    public CommonResult readFeedback(SetFeedbackParam setFeedbackParam) {

        Feedback feedback = baseMapper.selectOne(new QueryWrapper<Feedback>().eq(Feedback.ID, setFeedbackParam.getId()));
        if (null != feedback) {
            if (feedback.getReaded()) {
                return CommonResult.failed("该消息已经读取过了");
            } else {
                feedback.setReaded(true);
                feedback.setGmtModified(new Date());
                int count = baseMapper.updateById(feedback);
                if (count > 0) {
                    return CommonResult.success(null, "反馈消息读取成功");
                }
            }
        }
        return CommonResult.failed("该反馈消息不存在！");
    }
}
